package cn.lix.sc.elasticsearch.controller;

import cn.lix.sc.elasticsearch.aop.WpLogger;
import cn.lix.sc.elasticsearch.dto.Condition;
import cn.lix.sc.elasticsearch.dto.R;
import cn.lix.sc.elasticsearch.service.EsHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/es")
@RestController
public class EsHandleController {

    @Autowired
    private EsHandleService service;

    @WpLogger
    @GetMapping("/insertBatch")
    public R<String> insertBatch(@RequestParam("startId") Integer startId, @RequestParam("size") Integer size){
        service.insertBatch(startId, size);
        return R.success();
    }

    @WpLogger
    @GetMapping("/insert")
    public R<String> insert(@RequestParam("id") Integer id){
        service.insert(id);
        return R.success();
    }

    @WpLogger
    @GetMapping("/insertAsync")
    public R<String> insertAsync(@RequestParam("id") Integer id){
        service.insertAsync(id);
        return R.success();
    }

    @WpLogger
    @GetMapping("/existsIndex")
    public R<Boolean> existsIndex(@RequestParam("indexName") String indexName){
        return R.success(service.existsIndex(indexName));
    }


    @WpLogger
    @GetMapping("/deleteOne")
    public R<Boolean> deleteOne(@RequestParam("indexName") String indexName, @RequestParam("id") Integer id){
        return R.success(service.deleteOne(indexName, id));
    }

    @WpLogger(level = WpLogger.Level.WARN, output = true)
    @PostMapping("/search")
    public R<Boolean> search(@RequestBody List<Condition> fields){
        return R.success(service.search(fields));
    }

}
